﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WXshopBuiness;
using Services.WXshopBuiness;
using Services.Util;
using Model.Util;
using Xproject.Util;
using System.IO;
using Services.WXProjectAdmin;
using Model.Admin;
using Model.WXshopBuiness.Enum;
using Model.WeiXin.DTO;
using Core.WeiXin;
using Model.Const;

namespace WeiXinProject.Controllers
{
    public class SalesController : WXBaseController
    {
        WXProductServices m_WXProductServices;
        SalesServices m_SalesServices;
        public SalesController(  WXProductServices mWXProductServices,SalesServices mSalesServices)
        {
            m_WXProductServices = mWXProductServices;
            m_SalesServices = mSalesServices;
        }
        // GET: Sales
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MySalesBrandList()
        {
            ZXUserInfoModel userInfoModel = this.CurrentUser;
            ViewData["Products"] = new List<WXProduct>();
            string userId = this.CurrentUser.UserID;
            var usermodel = this.CurrentUser;
            if (userInfoModel != null && userInfoModel.ZX_UserType.HasValue && userInfoModel.ZX_UserType.Value == (int)EnumZX_UserType.SYSTEM_SALES)
            {
               List<WXProduct> Products = m_SalesServices.GetMySalesBrandList(Site, userInfoModel.ZX_UserID.ToString());
                ViewData["Products"]= Products;
            }
            ViewBag.basicInfo = usermodel;
            ViewBag.zxUserId = this.CurrentUser.ZX_UserID.HasValue?CurrentUser.ZX_UserID.Value.ToString():"0";
            return View();
        }
        [HttpPost]
        public JsonResult DownloadMyTGqrcode()
        {
            var jsonData = new Object();                 
            string productId = Request.Form["productId"];
            string pruductUrl = Request.Form["URL"];
            WXProduct product = m_WXProductServices.GetById(productId);
            if (string.IsNullOrEmpty(product.ImageUrl1))
            {
                jsonData = new { Success = false, Data = "产品没有图片！" };
                return Json(jsonData, JsonRequestBehavior.AllowGet);
            }
            string Virbase = @"Upload\Images\Sales\";
            string salesFolder = this.CurrentUser.UserID;
            string localpath = AppDomain.CurrentDomain.BaseDirectory + Virbase;
            string Folder = localpath + salesFolder;
            if (!Directory.Exists(Folder))
                Directory.CreateDirectory(Folder);
            string FileFullName = Folder + "\\"+productId + ".jpg";
            if (!System.IO.File.Exists(FileFullName))
            {
                QRcodeHelper.CreateProductTuiGuangQRcode(pruductUrl,
             product.Domain+product.ImageUrl1,
             product.ProductName,
             product.ShortDesc,
             product.SalePriceStr, product.MarketPrice.ToString("#0.00"),
             Folder, productId + ".jpg");
            }
            jsonData = new { Success = true, Data = GetDomain + "\\" + Virbase + salesFolder +"\\"+ productId + ".jpg" };
            return Json(jsonData, JsonRequestBehavior.AllowGet);         
        }
    }
} 